import 'package:connectivity/connectivity.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

class CommonsWidget {
  static ThemeData getThemeDataByName(name) {
    switch (name) {
      case 'light':
        return ThemeData.light();
      case 'dark':
        return ThemeData.dark();
      case 'blue':
        return ThemeData(primaryColor: Colors.blue);
      case 'deepOrange':
        return ThemeData(primaryColor: Colors.deepOrange);
      default:
        return ThemeData.light();
    }
  }

  static Widget getNullData(){
    return Center(
      child: Text('暂无数据...',style: TextStyle(color: Colors.grey,fontSize: 15.0),),
    );
  }

  static final Container hasDownLoad = Container(
    padding: EdgeInsets.symmetric(vertical: 3.0, horizontal: 5.0),
    child:Image(
        image: AssetImage("images/haveLoad2.png"),
        width: 30.0,
        height: 30.0,
    )
  ),
      notDownload = Container(
        padding: EdgeInsets.symmetric(vertical: 3.0, horizontal: 5.0),
        decoration: BoxDecoration(
          border: Border.all(
            color: Colors.red[400]
          ),
          borderRadius: BorderRadius.circular(5.0)
        ),
        child: Text('未下载',style: TextStyle(
          color: Colors.red[400],
        ),softWrap: true,),
      );

  static List<Widget> getIconsByNetWork(ConnectivityResult netWork) {
    switch (netWork) {
      case ConnectivityResult.wifi:
      case ConnectivityResult.mobile:
        return [
          Icon(
            netWork == ConnectivityResult.wifi
                ? Icons.wifi
                : Icons.import_export,
            color: Colors.white,
          ),
          Text(
            '在线',
            style: TextStyle(fontSize: 10.0, color: Colors.white),
          )
        ];
      case ConnectivityResult.none:
      default:
        return [
          Icon(
            Icons.phonelink_off,
            color: Colors.white,
          ),
          Text(
            '离线',
            style: TextStyle(fontSize: 10.0, color: Colors.white),
          )
        ];
    }
  }

  /// 单击提示退出
  static Future<bool> dialogExitApp(BuildContext context,String text) {
    return showDialog(
        context: context,
        builder: (context) => new AlertDialog(
              content: new Text("${text}"),
              actions: <Widget>[
                new FlatButton(
                    onPressed: () => Navigator.of(context).pop(false),
                    child: new Text("取消")),
                new FlatButton(
                    onPressed: () {
                      Navigator.of(context).pop(true);
                    },
                    child: new Text("确认"))
              ],
            ));
  }

  /// 提示框
  static Future<bool> confirmDialog(BuildContext context,String text) {
    return showDialog(
        context: context,
        builder: (context) => new AlertDialog(
              content: new Text(text),
              actions: <Widget>[
                new FlatButton(
                    onPressed: () =>  Navigator.pop(context, false),
                    child: new Text("取消")),
                new FlatButton(
                    onPressed: () =>  Navigator.pop(context, true),
                    child: new Text("确认"))
              ],
            ));
  }

  static Future<Null> showLoadingDialog(BuildContext context,String msg) async {
    await showDialog(
        context: context,
        builder: (BuildContext ctx) {
          return Material(
              color: Colors.transparent,
              child: WillPopScope(
                onWillPop: () => Future.value(true),
                child: Center(
                  child: new Container(
                    width: 200.0,
                    height: 200.0,
                    padding: new EdgeInsets.all(4.0),
                    decoration: new BoxDecoration(
                      color: Colors.transparent,
                      //用一个BoxDecoration装饰器提供背景图片
                      borderRadius: BorderRadius.all(Radius.circular(4.0)),
                    ),
                    child: new Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        new Container(
                            child: SpinKitCubeGrid(color: Colors.white)),
                        new Container(height: 10.0),
                        new Container(
                            child: new Text(
                          msg,
                          style: TextStyle(color: Colors.white),
                        )),
                      ],
                    ),
                  ),
                ),
              ));
        });
  }
}
